from PyQt5.QtWidgets import QLabel, QPushButton, QVBoxLayout, QWidget, QHBoxLayout

from utils.delLayout import delLayout


class TopBarParams:
    def __init__(self, newListener, settingListener, aboutListener, resetListener, fcfs_runListener, sjf_runListener):
        self.newListener = newListener
        self.settingListener = settingListener
        self.aboutListener = aboutListener
        self.resetListener = resetListener
        self.fcfs_runListener = fcfs_runListener
        self.sjf_runListener = sjf_runListener


class UI_Main:
    def __init__(self):
        self.output1 = None  # 输出
        self.output2 = None  # 初始化队列
        self.ready_list_show = None
        self.run_list_show = None
        self.finish_list_show = None
        self.time_show = None

    def refreshTime(self, time):
        self.time_show.setText("时间：{}".format(time))

    def resetWaitList(self, list_input):
        delLayout(self.ready_list_show)
        for item in list_input:
            obj = QPushButton("{}".format(item.id))
            self.ready_list_show.addWidget(obj)
        self.ready_list_show.addStretch()

    def resetRunList(self, list_input):
        delLayout(self.run_list_show)
        for item in list_input:
            obj = QPushButton("{}".format(item.id))
            self.run_list_show.addWidget(obj)
        self.run_list_show.addStretch()

    def resetFinishList(self, list_input):
        delLayout(self.finish_list_show)
        for item in list_input:
            obj = QPushButton("{}".format(item.id))
            self.finish_list_show.addWidget(obj)
        self.finish_list_show.addStretch()

    def flushOutput1(self, str_input):
        obj = QLabel(str_input)
        delLayout(self.output1)
        self.output1.addWidget(obj)

    def refreshInit(self, list_input):
        delLayout(self.output2)
        for item in list_input:
            obj = QPushButton("pid:{}   arrival:{}   server:{}".format(item.id, item.arrival_time, item.burst_time))
            self.output2.addWidget(obj)
        self.output2.addStretch()

    def createTopBar(self, top_bar_Params):
        container_widget = QWidget()
        container_widget.setStyleSheet("background-color: #FCE4EC;")

        top_bar = QHBoxLayout()

        newButton = QPushButton("随机生成")
        newButton.setStyleSheet("background-color: #3f50b5;")
        settingButton = QPushButton("新建")
        settingButton.setStyleSheet("background-color: #3f50b5;")
        aboutButton = QPushButton("关于")
        aboutButton.setStyleSheet("background-color: #3f50b5;")
        resetButton = QPushButton("重置")
        resetButton.setStyleSheet("background-color: #3f50b5;")

        fcfs_runButton = QPushButton("FCFS")
        fcfs_runButton.setStyleSheet("background-color: #EF9A9A;")
        sjf_runButton = QPushButton("SJF")
        sjf_runButton.setStyleSheet("background-color: #EF9A9A;")

        timeLabel = QLabel("时间: 0")
        self.time_show = timeLabel

        newButton.clicked.connect(lambda: top_bar_Params.newListener(self))
        settingButton.clicked.connect(lambda: top_bar_Params.settingListener(self))
        aboutButton.clicked.connect(top_bar_Params.aboutListener)
        resetButton.clicked.connect(lambda: top_bar_Params.resetListener(self))
        fcfs_runButton.clicked.connect(
            lambda: top_bar_Params.fcfs_runListener(self))
        sjf_runButton.clicked.connect(
            lambda: top_bar_Params.sjf_runListener(self))

        top_bar.addWidget(newButton)
        top_bar.addWidget(settingButton)
        top_bar.addWidget(aboutButton)
        top_bar.addWidget(resetButton)
        top_bar.addSpacing(40)
        top_bar.addWidget(fcfs_runButton)
        top_bar.addWidget(sjf_runButton)
        top_bar.addWidget(timeLabel)
        top_bar.addStretch()

        container_widget.setLayout(top_bar)
        return container_widget

    def createChart(self):
        chart_vg = QHBoxLayout()

        ready_cg_container = QWidget()
        run_cg_container = QWidget()
        finish_cg_container = QWidget()

        ready_cg_container.setStyleSheet("""
                background-color: #FFCDD2;
                border:4px solid #757de8; 
                border-top-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-left-radius:10px;
                border-bottom-right-radius:10px;
                padding:2px 4px;
                """)
        run_cg_container.setStyleSheet("""
                background-color: #FFCDD2;
                border:4px solid #757de8; 
                border-top-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-left-radius:10px;
                border-bottom-right-radius:10px;
                padding:2px 4px;
                """)
        finish_cg_container.setStyleSheet("""
                background-color: #FFCDD2;
                border:4px solid #757de8; 
                border-top-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-left-radius:10px;
                border-bottom-right-radius:10px;
                padding:2px 4px;
                """)

        ready_cg_container.setFixedHeight(400)
        run_cg_container.setFixedHeight(400)
        finish_cg_container.setFixedHeight(400)

        wait_cg = QVBoxLayout()
        run_cg = QVBoxLayout()
        finish_cg = QVBoxLayout()

        chart_vg.addLayout(wait_cg)
        chart_vg.addLayout(run_cg)
        chart_vg.addLayout(finish_cg)

        ################################
        wait_cg.addWidget(QLabel("就绪队列"))
        wait_cg.addWidget(ready_cg_container)

        ready_list = QVBoxLayout()
        ready_cg_container.setLayout(ready_list)
        self.ready_list_show = ready_list

        ################################
        ################################
        run_cg.addWidget(QLabel("运行队列"))
        run_cg.addWidget(run_cg_container)

        run_list = QVBoxLayout()
        run_cg_container.setLayout(run_list)
        self.run_list_show = run_list

        ################################
        ################################

        finish_cg.addWidget(QLabel("结束队列"))
        finish_cg.addWidget(finish_cg_container)

        finish_list = QVBoxLayout()
        finish_cg_container.setLayout(finish_list)
        self.finish_list_show = finish_list

        return chart_vg

    def createBottomBar(self):
        bottom_bar = QHBoxLayout()
        output_cg = QVBoxLayout()
        extra_cg = QVBoxLayout()
        bottom_bar.addLayout(output_cg)
        bottom_bar.addLayout(extra_cg)
        output_container = QWidget()
        output_content = QVBoxLayout()

        output_container.setStyleSheet("""
                background-color: #FFEBEE;
                border:4px solid #757de8; 
                border-top-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-left-radius:10px;
                border-bottom-right-radius:10px;
                padding:2px 4px;
                """)
        output_container.setFixedHeight(200)
        output_container.setLayout(output_content)
        output_cg.addWidget(QLabel("输出"))
        output_cg.addWidget(output_container)

        self.output1 = output_content

        extra_container = QWidget()
        extra_content = QVBoxLayout()

        extra_container.setStyleSheet("""
                    background-color: #FFEBEE;
                    border:4px solid #757de8; 
                    border-top-left-radius:10px;
                    border-top-right-radius:10px;
                    border-bottom-left-radius:10px;
                    border-bottom-right-radius:10px;
                    padding:2px 4px;
                    """)
        extra_container.setFixedHeight(200)
        extra_container.setLayout(extra_content)
        extra_cg.addWidget(QLabel("初始队列"))
        extra_cg.addWidget(extra_container)

        self.output2 = extra_content

        return bottom_bar

    def CreateMainWindow(self, screen, top_bar_params):
        win = QWidget()
        win.setWindowTitle("作业调度")
        width = 800
        height = 600
        win.setGeometry((screen.width - width) / 2, (screen.height - height) / 2, width, height)

        outline_vg = QVBoxLayout()

        top_bar = self.createTopBar(top_bar_params)
        chart = self.createChart()
        bottom_bar = self.createBottomBar()

        outline_vg.addWidget(top_bar)

        outline_vg.addStretch(1)

        outline_vg.addWidget(QLabel("队列: "))

        outline_vg.addLayout(chart)
        outline_vg.addStretch(30)

        outline_vg.addLayout(bottom_bar)

        win.setLayout(outline_vg)

        return win
